
insheet using "Chicago merged dataset.csv", c clear names

/** Summary statistics **/

forv i = 10/32 {
eststo res_`i': estpost summ cases_per_capita cumulative_cases_per_capita mar_releases_rate  poverty public_transit black_pct hisp_pct cumulative_case_rate_13 density if weeknum == `i' 
}
esttab res_* using rsummtabweek.csv, cells("count mean sd min max") noobs replace 

/** Weekly estimates - case rate **/

forv i = 10/32 {
eststo res_`i': regress cases_per_capita     mar_releases_rate poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i' 
}
esttab res_* using weeklyestimates.csv, replace

/** Weekly estimates - cumulative case rate **/

eststo clear
forv i = 10/32 {
eststo res_`i': regress cumulative_cases_per_capita     mar_releases_rate poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i' 
}
esttab res_* using cumulativeestimates.csv, replace 

/** Weekly and cumulative association trends **/

forv i = 10/32 {
     quietly regress cases_per_capita     mar_releases_rate poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i'
     estimate store rep78_`i'
 }
esttab rep78_* using estimates_pct_wkly.csv, replace


coefplot rep78_10 || rep78_11 || rep78_12 || rep78_13 || rep78_14 || rep78_15 || rep78_16 || rep78_17 || rep78_18 || rep78_19 || rep78_20 || rep78_21 || rep78_22 || rep78_23 || rep78_24 || rep78_25 || rep78_26 || rep78_27 || rep78_28 || rep78_29 || rep78_30 || rep78_31 || rep78_32, keep(mar_releases_rate) yline(0) bycoefs byopts(yrescale) vertical xline(4.5, lpattern(dash) lcolor(gray)) title("March Release and Weekly Covid-19 Cases (per capita)", size(medium)) xtitle("As of Week Starting (from March to August)", size(small)) ytitle("Partial Correlation between" "March Release and Weekly Covid-19 Cases (per capita)", size(small)) xlabel(1 "3/1" 2 "3/8" 3 "3/15" 4 "3/22" 5 "3/29" 6 "4/5" 7 "4/12" 8 "4/19" 9 "4/26" 10 "5/3" 11 "5/10" 12 "5/17" 13 "5/24" 14 "5/31" 15 "6/7" 16 "6/14" 17 "6/21" 18 "6/28" 19 "7/5" 20 "7/12" 21 "7/19" 22 "7/26" 23 "8/2", labsize(small) alt) yscale(titlegap(*5)) xscale(titlegap(*5))
graph export rrolling_zip.eps, replace
graph export rrolling_zip.png, replace



forv i = 10/32 {
     quietly regress cumulative_cases_per_capita	mar_releases_rate poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i'
     estimate store rep78_`i'
 }

coefplot rep78_10 || rep78_11 || rep78_12 || rep78_13 || rep78_14 || rep78_15 || rep78_16 || rep78_17 || rep78_18 || rep78_19 || rep78_20 || rep78_21 || rep78_22 || rep78_23 || rep78_24 || rep78_25 || rep78_26 || rep78_27 || rep78_28 || rep78_29 || rep78_30 || rep78_31 || rep78_32, keep(mar_releases_rate) yline(0) bycoefs byopts(yrescale) vertical xline(4.5, lpattern(dash) lcolor(gray)) title("March Release and Cumulative Covid-19 Cases (per capita)", size(medium)) xtitle("As of Week Starting (from March to August)",size(small)) ytitle("Partial Correlation between" "March Release and Cumulative Covid-19 Cases (per capita)", size(small)) xlabel(1 "3/1" 2 "3/8" 3 "3/15" 4 "3/22" 5 "3/29" 6 "4/5" 7 "4/12" 8 "4/19" 9 "4/26" 10 "5/3" 11 "5/10" 12 "5/17" 13 "5/24" 14 "5/31" 15 "6/7" 16 "6/14" 17 "6/21" 18 "6/28" 19 "7/5" 20 "7/12" 21 "7/19" 22 "7/26" 23 "8/2", labsize(small) alt) yscale(titlegap(*5)) xscale(titlegap(*5))
graph export rcumulative_zip.eps, replace
graph export rcumulative_zip.png, replace

/** Estimates including testing rate as control **/

forv i = 10/32 {
     quietly regress cumulative_cases_per_capita	mar_releases_rate cumulative_tests_per_capita poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i'
     estimate store rep78_`i'
 }
esttab rep78_* using estimates_cumulative_testing.csv, replace

/** Estimates with case rates in adjacent zips as control **/

forv i = 10/32 {
     quietly regress cumulative_cases_per_capita	mar_releases_rate mar_releases_donut_rate poverty public_transit  black_pct hisp_pct density cumulative_case_rate_13 if weeknum==`i'
     estimate store rep78_`i'
 }
esttab rep78_* using estimates_cumulative_adjacent.csv, replace


/** Codebook for variable names **/

quietly {
    log using filename_codebook.txt, text replace
    noisily codebook black_pct hisp_pct poverty public_transit density white_pct-xhhs7ormorepersonhh, compact
    log close
}


/* Principal components analysis and MLR */
// drop pc*
pca  poverty public_transit  density black_pct cumulative_case_rate_13  white_pct-xhhs7ormorepersonhh  if weeknum==32
predict pc1 pc2 pc3 pc4 pc5 pc6 pc7 pc8 pc9 pc10 pc11 pc12 pc13 pc14 pc15 pc16 pc17 pc18 pc19 pc20, score


regress cumulative_cases_per_capita mar_releases_rate  pc1 pc2 pc3 pc4 pc5 pc6 pc7 pc8 pc9 pc10 pc11 pc12 pc13 pc14 pc15 pc16 pc17 pc18 pc19 pc20 if weeknum==32
eststo mlrresult
esttab mlrresult using "pca_mlr.csv", nogaps not replace

/* Elastic net regression using PCA controls */

asdoc elasticnet linear cumulative_cases_per_capita mar_releases_rate  pc1 pc2 pc3 pc4 pc5 pc6 pc7 pc8 pc9 pc10 pc11 pc12 pc13 pc14 pc15 pc16 pc17 pc18 pc19 pc20 if weeknum==32, rseed(1) dec(7)
lassocoef, display(coef, post) sort(coef, post)
matrix M = r(coef)
putexcel set "pca_lasso.xlsx", sheet("postselection results") replace
putexcel A1=matrix(M), rownames

/* OLS using the lasso postselection */

regress cumulative_cases_per_capita mar_releases_rate  pc20 pc3 pc5 pc13 pc2 pc15 pc17 pc1 pc14 pc12 pc18 pc7 pc4 pc16 pc8 pc6 pc9 if weeknum==32
eststo mlrresult
esttab mlrresult using "elastic_mlr.csv", nogaps not replace
